/**
 * 
 */
var countdown=60; 
function settime(obj) { 
    if (countdown == 0) { 
    	$(obj).css('cursor','pointer');
        obj.value="获取验证码"; 
        return;
    } else { 
        obj.value="重新发送(" + countdown + ")"; 
        countdown--; 
    } 
setTimeout(function() { 
    settime(obj) }
    ,1000) 
}
function send(id,obj){
	var tel='';
	if(id==1){
		tel='&tel='+$('#signupform-tel').val();
	}
	if(id==5){
		tel='&tel='+$('#safeform-tel').val();
	}
	if(id==3){
		tel='&name='+$('#resetpwdform-name').val();
	}
	if($(obj).css('cursor')=='not-allowed') return;
	countdown=60;
	settime(obj);
	$(obj).css('cursor','not-allowed');
    $.ajax({
    	type:'post',
    	url:'/site/send-code.html?id='+id+tel,
    	success:function(data){
    		if(data==1){
    			layer.msg('发送成功',{icon: 1});
    		}else{
    			layer.msg('发送失败,如果尝试多次失败请联系客服,错误代码'+data,{icon: 2});
    		}
    	}
    });
}
/**
 * 递归加载省市区数据
 * @param {int} pid 父级id
 * @param {string} child  input的ID值
 * @param {int} num 在主循环数组中的键值
 * @returns {undefined}
 */
function showArea(pid,child,num){
	if(child==undefined) return;
    $.ajax({
        type:'post',
        url:'/site/area.html',
        data:'pid='+pid,
        dataType:'json',
        success:function(data){
            var str='';
             var index=area[num][0].indexOf(child);
             var val=data[0]['id'];
             var select='';
            $.each(data,function(k,v){
                select='';
                if(area[num][1][index]==v.id){
                    val=v.id;
                    select='selected';
                }
                str+='<option value="'+v.id+'" '+select+'>'+v.name+'</option>';
            })
            $(child).html('');
            $(child).append(str);
            if(index==area[num][0].length-1) return;
            showArea(val,area[num][0][index+1],num);
        }
    });
}

/**
 * 添加省市区的联动事件
 * @param {type} data  select的ID数组
 * @param {type} num    主数组中的键
 * @returns {undefined}
 */
function addArea(data,num){
    for(var i=0;i<data.length-1;i++){
        $(data[i]).change(function(){
            showArea($(this).val(),$(this).attr('child'),$(this).attr('num'));
        });
        $(data[i]).attr('child',data[i+1])
        $(data[i]).attr('num',num)
    }
}

/**
 * 单个删除
 * @param obj
 * @param id
 */
function del(obj,id,mv){
	layer.confirm('确定要删除么', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			$.ajax({
				type:'post',
				url:'delete.html?id='+id,
				dataType:'json',
				success:function(data){
					if(data==1){
						layer.close(layer.index);
						mv.remove();
					}else{
						layer.msg('删除失败', {icon: 2});
					}
				}
			});
		});
}
/**
 * 收藏
 * @param id
 */
function collect(id){
	$.ajax({
		type:'post',
		url:'/goods/collect.html',
		data:'id='+id,
		dataType:'json',
		success:function(data){
			switch(data[0]){
			case 0:layer.msg('请先登录',{icon: 2,skin: 'layer-ext-espresso'});break;
			case 1:layer.msg('收藏成功',{icon: 1,skin: 'layer-ext-espresso'});break;
			case 2:layer.msg('收藏失败,系统繁忙',{icon: 1,skin: 'layer-ext-espresso'});break;
			case 3:layer.msg('收藏失败,商品不存在',{icon: 1,skin: 'layer-ext-espresso'});break;
			case 4:layer.msg('您已收藏过该宝贝',{icon: 1,skin: 'layer-ext-espresso'});break;
			}
		}
	});
}
function keynum(obj){
	var num=$(obj).val();
	if(num=='') return;
	var reg=new RegExp("^[1-9]\\d*$").test(num);
	if(!reg){
		$(obj).val(1);
	}
}